package com.usian.controller;

import com.usian.pojo.Dept;
import com.usian.pojo.Emp;
import com.usian.pojo.Empvo;
import com.usian.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class EmpController {
    @Autowired
    private EmpService empService;
    @RequestMapping("findall")
    public String show(Emp emp, Model model){
        List<Empvo> findall = empService.findall(emp);
        model.addAttribute("list",findall);
        return "findall";
    }
    @RequestMapping("toadd")
    public String toadd(HttpServletRequest request){
        List<Dept> deptList=empService.finddept();
        request.setAttribute("dept",deptList);
        return "add";
    }
    @RequestMapping("add")
    public String add(Emp emp){
        empService.add(emp);
        return "redirect:findall";
    }
    @RequestMapping("deletebyeid")
    public String deletebyeid(Integer id){
        empService.deletebyeid(id);
        return "redirect:findall";
    }
    @RequestMapping("findbyid")
    public String findbyid(Integer id, HttpServletRequest request){
        Emp emp=empService.findbyid(id);
        List<Dept> deptList=empService.finddept();
        request.setAttribute("dept",deptList);
        request.setAttribute("emp",emp);
        return "update";
    }
    @RequestMapping("update")
    public String update(Emp emp){
        empService.updatebyemp(emp);
        return "redirect:findall";
    }


}
